<template>
  <div>
    <!-- title -->
    <PageTitle>
      {{ $t("menu.M_10400412") }}
    </PageTitle>
    <!-- Table model -->
    <el-card
      shadow="never"
      :body-style="{ padding: '0px' }"
      @current-change="currentChangeHandle"
      :page-size="pageSize"
      :total="totalPage"
      :current-page="pageIndex"
    >
      <el-form @submit.prevent.native class="p-4" inline>
        <el-form-item>
          <el-input
            v-model="ticketTitle"
            :maxlength="32"
            show-word-limit
            @blur="() => (ticketTitle = ticketTitle.trim())"
            :placeholder="$t('ticket.title')"
            clearable
            @keyup.enter.native="search((pageIndex = 1))"
          />
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="status"
            clearable
            :placeholder="$t('ticket.status')"
            @change="statusChange"
          >
            <el-option
              v-for="item in statusArray"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="w-8" @click="search((pageIndex = 1))">
            <svg-icon icon-class="search" />
          </el-button>
        </el-form-item>
      </el-form>
      <el-table :v-loading="listLoading" :data="ticketData" stripe>
        <el-table-column :label="$t('ticket.title')" prop="title" min-width="200">
          <template v-slot="{ row }">
            <el-link @click="ticketInfo(row)">{{ row.title }}</el-link>
          </template>
        </el-table-column>
        <el-table-column
          prop="companyName"
          min-width="100"
          :label="$t('developer.name')"
        />
        <el-table-column
          prop="customerName"
          min-width="100"
          :label="$t('developer.account_name')"
        />
        <el-table-column
          prop="countryName"
          min-width="100"
          :label="$t('user.country_or_region')"
        />
        <el-table-column prop="agentName" min-width="100" :label="$t('ticket.handler')" />
        <el-table-column :label="$t('ticket.status')" prop="status" width="150px">
          <template v-slot="{ row }">
            <el-tag v-if="row.status === 4" size="small" type="success">{{
              statusFilter(row.status)
            }}</el-tag>
            <el-tag v-if="row.status !== 4" size="small" type="private">{{
              statusFilter(row.status)
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column :label="$t('common.apply_time')" prop="updTime" width="200px">
          <template v-slot="{ row }">
            <div :title="timeFilter(row.updTimeStamp)">
              {{ dateFilter(row.updTimeStamp) }}
            </div>
          </template>
        </el-table-column>

        <!--        <el-table-column width="250px">-->
        <!--          <template v-slot="scope">-->
        <!--            <el-rate-->
        <!--              v-if="scope.row.evaluateRate"-->
        <!--              v-model="scope.row.evaluateRate"-->
        <!--              disabled-->
        <!--              :colors="colors">-->
        <!--            </el-rate>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
      </el-table>
    </el-card>
    <ticket-detail-drawer
      v-if="ticketDetailDrawerVisible"
      ref="ticketDetailDrawer"
      size="60%"
      v-model="ticketDetailDrawerVisible"
    />
  </div>
</template>

<script>
import { timeFilter, dateFilter } from "@/filters";
import { getTicketList } from "@/api/ticket";
import TicketDetailDrawer from "@/views/developer/components/ticket-detail-drawer";
import { HTMLDecode, removeHtmlStyle } from "@/utils";
import PageTitle from "@/components/PageTitle/index.vue";
export default {
  props: {
    isGrid: {
      type: Boolean,
      default: true,
    },
  },
  components: {
    TicketDetailDrawer,
    PageTitle,
  },
  data() {
    return {
      listLoading: false,
      colors: ["#99A9BF", "#F7BA2A", "#FF9900"],
      ticketTitle: "",
      ticketContent: "",
      status: "",
      ticketDetailDrawerVisible: false,
      ticketEvaluateVisible: false,
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      ticketId: "",
      ticketData: [],
    };
  },
  computed: {
    statusArray() {
      return [
        {
          label: this.$t("ticket.status_list.new"),
          value: 1,
        },
        {
          label: this.$t("ticket.status_list.in_progress"),
          value: 2,
        },
        {
          label: this.$t("ticket.status_list.on_hold"),
          value: 3,
        },
        {
          label: this.$t("ticket.status_list.resolved"),
          value: 4,
        },
        {
          label: this.$t("ticket.status_list.closed"),
          value: 5,
        },
        {
          label: this.$t("ticket.status_list.need_reply"),
          value: 6,
        },
        {
          label: this.$t("ticket.status_list.in_verification"),
          value: 7,
        },
      ];
    },
  },
  mounted() {
    if (this.$route.query.status) {
      this.status = Number(this.$route.query.status);
    }
    this.search();
  },
  methods: {
    timeFilter,
    dateFilter,
    statusFilter(status) {
      const statusMap = {
        1: this.$t("ticket.status_list.new"),
        2: this.$t("ticket.status_list.in_progress"),
        3: this.$t("ticket.status_list.on_hold"),
        4: this.$t("ticket.status_list.resolved"),
        5: this.$t("ticket.status_list.closed"),
        6: this.$t("ticket.status_list.need_reply"),
        7: this.$t("ticket.status_list.in_verification"),
      };
      return statusMap[status];
    },
    search() {
      this.loading = true;
      getTicketList({
        pageNo: this.pageIndex,
        pageSize: this.pageSize,
        title: this.ticketTitle,
        content: this.ticketContent,
        status: this.status,
      })
        .then((res) => {
          if (res) {
            this.loading = false;
            this.ticketData = res.data;
            this.totalPage = res.rows;
            if (this.ticketData) {
              this.ticketData.forEach((item) => {
                item.content = HTMLDecode(removeHtmlStyle(item.content)).replace(
                  /(<img([\s\S]*)>)|(<p>)|<\/p>/g,
                  ""
                );
                item.content = removeHtmlStyle(item.content);
              });
            }
          }
        })
        .catch(this.$confirmError);
    },
    statusChange(value) {
      this.status = value;
      this.search((this.pageIndex = 1));
    },
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.search();
    },
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.search();
    },
    ticketInfo(ticket) {
      this.ticketDetailDrawerVisible = true;
      this.$nextTick(() => {
        this.$refs.ticketDetailDrawer.init(ticket);
      });
    },
    removeHtmlStyle,
  },
};
</script>

<style lang="scss">
.my-app-popover {
  min-width: 100px !important;
}
</style>

<style lang="scss" scoped>
.upload-app {
  @apply border-2;
  border-color: #b1c2d9;
  width: 72px;
  height: 72px;
}
</style>
